//
//  ZZTongUtility.m
//  ZZTong
//
//  Created by xy on 13-5-20.
//  Copyright (c) 2013年 handpay. All rights reserved.
//

#import "ZZTongUtility.h"
#import "CategoryUtil.h"
#import "HPUIEngine.h"
#import "Utility.h"
#import "CUtility.h"

@interface ZZTongUtility (){
    
}
@property(nonatomic,retain) NSDictionary* cardBinDic;
@property(nonatomic,retain) NSDictionary* logoImageDic;

@end

@implementation ZZTongUtility

//static NSArray* cmbcCardBinArray = nil;
//static NSArray* abcCardBinArray = nil;

static ZZTongUtility* instance = nil;


- (BOOL)isBankCard:(NSString*)cardNo withName:(BANK_FLAG)flag{
    
    if ([NSString isNilOrEmpty:cardNo]) {
        return NO;
    }
    
    //查找所有银行
    if (flag == BANK_NONE) {
        int bankCount = 4;
        BOOL isBank = NO;
        for(int i = 1; i < bankCount; i++){
            isBank = [self isBankCard:cardNo withName:i];
            if (isBank) {
                return isBank;
            }
        }
        return isBank;
    }
    
    //
    NSString* name = [self getBankName:flag];
    if (!name) {
        return NO;
    }
    NSArray* array = (NSArray*)[self.cardBinDic objectForKey:name];
    
    for (NSString* cardBin in array) {
        if ([cardNo hasPrefix:cardBin]) {
            return YES;
        }
    }
    return NO;
}
- (NSString*)getBankLogoWithName:(NSString*)name{
    return [self.logoImageDic objectForKey:name];
}
- (NSString*)getBankLogo:(NSString*)cardNo{
   
    if ([NSString isNilOrEmpty:cardNo]) {
        return nil;
    }
    
    NSString* bankName = @"未知银行";
    
    for (NSString* strKey in self.cardBinDic.allKeys) {
        NSArray* array = (NSArray*)[self.cardBinDic objectForKey:strKey];
        for (NSString* cardBin in array) {
            if ([cardNo hasPrefix:cardBin]) {
                bankName = strKey;
                break;
            }
        }
    }
    
    return [self.logoImageDic objectForKey:bankName];
}
+(ZZTongUtility *)getZZTongUtility{
	if (!instance) {
		instance = [[ZZTongUtility alloc] init];
	}
	return instance;
}
- (void)dealloc{
    self.logoImageDic = nil;
    self.cardBinDic = nil;
    [super dealloc];
}
- (id)init{
    self = [super init];
    if (self) {
        self.cardBinDic = @{@"民生银行" : @[@"421393",
                                   @"472067",
                                   @"472068",
                                   @"427571",
                                   @"427570",
                                   @"421865",
                                   @"415599",
                                   @"622615",
                                   @"622617",
                                   @"622619",
                                   @"622622",
                                   @"622616",
                                   @"622620",
                                   @"622618",
                                   @"621691"],
                       @"农业银行" : @[@"95599",
                                   @"622848",
                                   @"622847",
                                   @"622845",
                                   @"622844",
                                   @"622840",
                                   @"622825",
                                   @"622824",
                                   @"622823",
                                   @"622822",
                                   @"622821",
                                   @"558730",
                                   @"552599",
                                   @"520082",
                                   @"520083",
                                   @"519412",
                                   @"519413",
                                   @"404117",
                                   @"404121",
                                   @"403361",
                                   @"53591",
                                   @"49102"],
                       @"晋商银行" : @[@"621216",
                                   @"622465",
                                   @"940031",
                                   @"623179"]};
        
//        self.logoImageDic = @{@"民生银行" : @"cmbc.png",
//                         @"农业银行" : @"abc.png",
//                         @"晋商银行" : @"jsb.png",
//                         @"默认" : @"bankDefault.png"};
        
        self.logoImageDic = @{@"农业银行" : @"abc.png",
                              @"中国银行" : @"bc.png",
                              @"北京银行" : @"bob.png",
                              @"交通银行" : @"boc.png",
                              @"宁波银行" : @"bon.png",
                              @"上海银行" : @"bos.png",
                              @"包商银行" : @"bsb.png",
                              @"建设银行" : @"ccb.png",
                              @"光大银行" : @"ceb.png",
                              @"广东发展银行" : @"cgb.png",
                              @"招商银行" : @"cmb.png",
                              @"民生银行" : @"cmbc.png",
                              @"中国邮政储蓄银行" : @"cp.png",
                              @"东亚银行" : @"dy.png",
                              @"农村信用合作社" : @"gzls.png",
                              @"哈尔滨银行" :  @"hrb.png",
                              @"华夏银行" : @"hx.png",
                              @"工商银行" : @"icbc.png",
                              @"兴业银行" : @"isb.png",
                              @"中国平安银行" : @"pa.png",
                              @"深圳发展银行" : @"sdb.png",
                              @"上海农商银行" : @"shns.png",
                              @"浦东发展银行" : @"spdb.png",
                              @"浙江泰融商业银行" : @"zjtl.png",
                              @"中信银行" : @"zx.png",
                              @"南京银行" : @"njcb.png",
                              @"未知银行" : @"bankDefault.png"};
    }
    
    return self;
}
- (NSString*)getBankName:(BANK_FLAG)flag{
    NSString* bankName = nil;
    switch (flag) {
        case BANK_ABC:
            bankName = @"农业银行";
            break;
        case BANK_CMBC:
            bankName = @"民生银行";
            break;
        case BANK_JS:
            bankName = @"晋商银行";
            break;
        default:
            bankName = @"银行";
            break;
    }
    return bankName;
}

+ (BOOL)checkIsInput:(UITextField*)textField tips:(NSString*)tips{
    NSString* text = textField.text;
    if ([NSString isNilOrEmpty:text]) {
        [HPUIENG showAlert:[@"请输入" stringByAppendingString:tips]];
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}
+ (BOOL)checkName:(NSString*)name tips:(NSString *)tip{
    if ([NSString isNilOrEmpty:name]) {
        [HPUIENG showAlert:[@"请输入" stringByAppendingString:tip]];
        return NO;
    }
    NSRange range = [name rangeOfString:@"[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]" options:NSRegularExpressionSearch];
    if (range.location != NSNotFound) {
        [HPUIENG showAlert:[@"请输入正确的" stringByAppendingString:tip]];
        return NO;
    }
    return YES;
}

+ (BOOL)checkShopName:(NSString*)name tips:(NSString *)tip{
    if ([NSString isNilOrEmpty:name]) {
        [HPUIENG showAlert:[@"请输入" stringByAppendingString:tip]];
        return NO;
    }
    NSRange range = [name rangeOfString:@"[`~!@#$%^&*+=|{}':;'\\[\\]<>/?~！@#￥%……&*——+|{}【】‘；：”“’。，、？]" options:NSRegularExpressionSearch];
    if (range.location != NSNotFound) {
        [HPUIENG showAlert:[@"请输入正确的" stringByAppendingString:tip]];
        return NO;
    }
    return YES;
}

//当前时间是否是在交易时间当天的22.55点之前
+ (BOOL)compareTime:(NSString*)transTime{
    return YES;
    
//    transTime = [transTime substringToIndex:8];
//    NSString* nowTime = [Utility getNowDateTime];
//    NSString* nowDate = [nowTime substringToIndex:8];
//    
//    NSComparisonResult dateResult =[nowDate compare:transTime options:NSNumericSearch];
//    
//    
//    if (dateResult == NSOrderedSame) {
//        nowTime = [nowTime substringFromIndex:8];
//        if ([nowTime compare:@"225500" options:NSNumericSearch] == NSOrderedAscending) {
//            return YES;
//        }
//    }
//    return NO;
}

+ (BOOL)checkLength:(UITextField*)textField tips:(NSString*)tips minLen:(NSInteger)minLen maxLen:(NSInteger)maxLen {
    NSString* text = textField.text;
    if (text.length < minLen || text.length > maxLen) {
        [HPUIENG showAlert:tips];
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}

+ (BOOL)checkMobile:(NSString*)mobile tips:(NSString*)tips{
    if ([NSString isNilOrEmpty:mobile]) {
        [HPUIENG showAlert:tips];
        return NO;
    }
    
    if (mobile.length != 11) {
        [HPUIENG showAlert:tips];
        return NO;
    }
    
    if (![Utility isPureInt:mobile]) {
        [HPUIENG showAlert:tips];
        return NO;
    }
    
    if (![mobile hasPrefix:@"1"]) {
        [HPUIENG showAlert:tips];
        return NO;
    }
    
    return YES;
}

+ (BOOL)checkIDCard:(NSString *)number tips:(NSString*)tips
{
    NSString * msg = [Utility IDCardValidate:number];
    if (msg)
    {
        [HPUIENG showAlert:tips];
        return NO;
    }
    return YES;
}

+ (NSString*)maskMobile:(NSString*)mobile{
   
    if ([NSString isNilOrEmpty:mobile]) {
        return nil;
    }
    mobile = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
//    if (mobile.length != 11) {
//        LOG(@"mobile length is not 11");
//        return nil;
//    }
    
    NSString* result = [NSString stringWithFormat:@"%@****%@", [mobile substringToIndex:3], [mobile substringFromIndex:7]];
    return result;
}

+ (NSString*)packageData:(NSString*)data{
    
    char tempData[512] = {0};
    int index = 0;
    tempData[index++] = data.length;
    memcpy(tempData + index, [data UTF8String], data.length);
    index += data.length;
    
    tempData[index++] = 0x80;
    int fixLen = index % 8;
    if (fixLen != 0) {
        fixLen = 8 - fixLen;
    }
    
    char result[512] = {0};
    
    hexDecode((const unsigned char*)tempData, index + fixLen, result);
    
    return [[[NSString alloc] initWithCString:result encoding:NSUTF8StringEncoding]autorelease];
}

+ (NSString*)packageToEncry:(NSString*)data isMac:(BOOL)isMac{
    char tempData[512] = {0};
    int index = 0;
    memcpy(tempData, [data UTF8String], data.length);
    index += data.length;
    if (isMac) {
        tempData[index++] = 0x80;
    }
    char result[512] = {0};
    int fixLen = index % 8;
    if (fixLen != 0) {
        fixLen = 8 - fixLen;
    }
    
    hexDecode((const unsigned char*)tempData, index + fixLen, result);
    return [[NSString alloc] initWithCString:result encoding:NSUTF8StringEncoding];
}

+ (NSString *)stringToHex:(NSString *)string
{
    NSMutableString * result = [NSMutableString string];
    for (NSInteger index = 0; index < string.length; index++)
    {
        unichar c = [string characterAtIndex:index];
        [result appendFormat:@"%x",c];
    }
    return result;
}

@end
